#pragma once
#include <brpc/server.h>
#include <butil/logging.h>
#include "logger.hpp"  //日志输出
#include "etcd.hpp"    //服务注册
#include "asr.hpp"     //语音识别服务
#include "speech.pb.h" //proto调用框架

namespace my_im
{
    class SpeechServiceImpl : SpeechService
    {
    public:
        SpeechServiceImpl(const my_im::ASRClient::ptr &client) : _client(client) {}
        ~SpeechServiceImpl() {};
        void SpeechRecognition(::google::protobuf::RpcController *controller,
                               const ::example::EchoRequest *request,
                               ::example::EchoResponse *response,
                               ::google::protobuf::Closure *done) override
        {
            brpc::ClosureGuard rpc_guard(done);
            // 取出语音数据并调用sdk模块语言识别
            std::string err;
            std::string res = _client->recognize(request->speech_context(), err);
            // 构建回复消息
            if (res.empty())
            {
                ERR("{}语音识别失败", request->request_id());
                response->set_response_id(request->request_id());
                response->set_success(false);
                response->set_errmsg("语言识别失败：" + err);
                return;
            }
            response->set_response_id(request->request_id());
            response->set_success(true);
            response->set_recognition_result(res);
        }

    private:
        ASRClient::ptr _client;
    };

    class SpeechServe
    {
    public:
        using ptr = std::make_shared<SpeechServe>;
        SpeechServe(const ASRClient::ptr aclient,
                    const Registry::ptr rclient,
                    const shared_ptr<brpc::Server> server)
            : _asr_client(aclient), _reg_client(rclient), _server(server) {}
        ~SpeechServe() {}
        void start() {_server->RunUntilAskedToQuit();}

    private:
        ASRClient::ptr _asr_client;
        Registry::ptr _reg_client;
        shared_ptr<brpc::Server> _server;
    };
    class SpeechServeBuild
    {
    public:
        //构造语音识别对象
        void make_asr_object(const std::string &app,const std::string &api,const std::string &secret)
        {
            _asr_client = std::make_shared<ASRClient>(app,api,secret);
        }
        //用于构造服务注册对象
        void make_reg_object(const std::string reg_host,
                             const std::string access_host,
                             const std::string server_name)
        {
            _reg_client = std::make_shared<Registry>(reg_host);
            _reg_client->registry(server_name,access_host);
        }
        //rpc信道服务器
        void make_reg_object(uint32_t port,int32_t timeout,uint8_t num_threads)
        {
            _server = std::make_shared<brpc::Server>();
            brpc::ServerOptions option;
            option.idle_timeout_sec = timeout;
            option.num_thread = num_threads;
            int ret = _server->Start(port,&option);
            if(ret == -1)
            {
                ERR("服务器启动失败");
            }
        }

        SpeechServe::ptr build()
        {
            if(!_asr_client)
            {
                ERR("未初始化语言识别模块");
                abort();
            }
            if(!_reg_client)
            {
                ERR("未初始化服务注册模块");
                abort();
            }
            if(!_server)
            {
                ERR("未初始化rpc信道模块");
                abort();
            }
            return std::make_shared<brpc::Server>(_asr_client,_reg_client,_server);
        }
    private:
        ASRClient::ptr _asr_client;
        Registry::ptr _reg_client;
        shared_ptr<brpc::Server> _server;
    };
}
